package com.ebocy.hammer.world.message;

import java.util.List;

import com.ebocy.hammer.core.constants.GameMessageCode;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.GamePlayerItem;
import com.ebocy.hammer.core.message.AbstractS2CMessage;
import com.ebocy.hammer.core.util.ServerAddress;

public class S2CPlayerLogin extends AbstractS2CMessage {
	private GamePlayer gamePlayer;
	private String serverIp;
	private short serverPort;

	public S2CPlayerLogin(GamePlayer gamePlayer, ServerAddress gameServer) {
		this.serverIp = gameServer.getIp();
		this.serverPort = gameServer.getPort();
		this.gamePlayer = gamePlayer;
	}

	@Override
	protected void writeMessage() {
		// 服务器信息
		writeString(serverIp);
		writeShort(serverPort);

		// 用户游戏信息
		writeString(String.valueOf(gamePlayer.getGamePlayerId()));
		writeString(gamePlayer.getName());
		writeString(gamePlayer.getHeadPortrait());
		List<GamePlayerItem> itemList = gamePlayer.getKnapsack();
		
		//用户背包信息
		writeShort((short)itemList.size());
		for(GamePlayerItem item:itemList){
			//物品唯一id
			writeString(String.valueOf(item.getGamePlayerItemId()));
			//物品id
			writeInt(item.getItemId());
			//创建时间
			writeString(String.valueOf(item.getCreateTime()));
			//过期时间,-1为永久道具
			writeShort(item.getEffectiveTime());
			//是否装备,1为装备
			writeInt(item.getKey());
		}
		
		writeShort(gamePlayer.getLevel());
		writeInt(gamePlayer.getExp());
		writeString(String.valueOf(gamePlayer.getSumPoints()));
		writeInt(gamePlayer.geteGold());
		writeInt(gamePlayer.getSumPerBattleTimes());
		writeInt(gamePlayer.getSumTeamBattleTimes());
		writeInt(gamePlayer.getSumFirstPerBattleTimes());
		writeInt(gamePlayer.getSumFirstTeamBattleTimes());
		writeInt(gamePlayer.getSumKillPerBattleTimes());
		writeInt(gamePlayer.getSumKillTeamBattleTimes());
		writeString(gamePlayer.getFriendIds() == null ? "" : gamePlayer.getFriendIds());
		writeInt(gamePlayer.getEffort());
		writeInt(gamePlayer.getGold());
	}

	@Override
	public short getMessageCode() {
		return GameMessageCode.LOGIN;
	}

}
